Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in noImplicitReturns mode, also disallow "return;" #7358

Merged
merged 1 commit into from
Mar 3, 2016
Merged

in noImplicitReturns mode, also disallow "return;" #7358

merged 1 commit into from
Mar 3, 2016

Conversation

evmar
Copy link
Contributor

@evmar evmar commented Mar 3, 2016

In --noImplicitReturns mode, if a function specifies a return type,
disallow empty "return;" statements.

Fixes #5916.


This is my first TypeScript change, so please review with caution.

Some things wasn't sure about, checked off the resolved ones:

  • Should I add more tests for other cases? E.g. a function that is tagged with void return etc.
  • Should I use a different error message?
  • Maybe my check against Any here is wrong? I copied it from elsewhere.

@msftclas
Copy link

msftclas commented Mar 3, 2016

Hi @martine, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by Microsoft and real humans are currently evaluating your PR.

TTYL, MSBOT;

@evmar
Copy link
Contributor Author

evmar commented Mar 3, 2016

The Travis build appears to have failed due to a lint error (?).

I don't see any mention of lint in CONTRIBUTING.md, suggestions welcome!

@@ -13880,6 +13880,11 @@ namespace ts {
checkTypeAssignableTo(exprType, returnType, node.expression);
}
}
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Else on the next line (we link to a style guide in CONTRIBUTING.md)

@DanielRosenwasser
Copy link
Member

Can you rename the test file? It's not that there's an empty statement, it's that the return statement lacks a return expression.

Other than that and a few style nits, this looks good. @vladima, can you also take a look?

@DanielRosenwasser
Copy link
Member

Also, jake lint will run our linter.

@evmar
Copy link
Contributor Author

evmar commented Mar 3, 2016

So embarrassing that I got tabs wrong, it's such a pet peeve of mine when others get it wrong!

I think I have fixed the issues you brought up. I'm still not quite sure I'm checking for the right thing (e.g. isTypeOfKind vs maybeTypeOfKind) but I added some more test cases to verify it makes some sense.

@@ -13881,6 +13881,10 @@ namespace ts {
}
}
}
else if (compilerOptions.noImplicitReturns && !isTypeOfKind(returnType, TypeFlags.Void)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void | any. similar to other places where we check noImpicitReturns.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your suggestion, this code is now accepted. Is that expected? (I don't have a good intuition about how "any" ought to behave, so I just wanted to double check.)

    function isMissingReturnExpression2(): any {
      return;
    }

I have made the change regardless.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 3, 2016

also can you add a test for an implicit return type that is inferred from the function body:

function f(x) {
   if (x) {
       return 0;
   }
   else {
       return;
   }
}

In --noImplicitReturns mode, if a function specifies a return type,
disallow empty "return;" statements.

Fixes #5916.
@evmar
Copy link
Contributor Author

evmar commented Mar 3, 2016

Done. I also fixed the tabs in my test code (adding the "if" statement made me realize I had it wrong).

@vladima
Copy link
Contributor

vladima commented Mar 3, 2016

👍

@mhegazy
Copy link
Contributor

mhegazy commented Mar 3, 2016

Wow.. @traviceCI is taking its time for this commit..

@DanielRosenwasser
Copy link
Member

The suspense...

mhegazy added a commit that referenced this pull request Mar 3, 2016
in noImplicitReturns mode, also disallow "return;"
@mhegazy mhegazy merged commit 0cba37d into microsoft:master Mar 3, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Mar 3, 2016

thanks @martine!

@evmar evmar deleted the empty-return branch March 3, 2016 22:00
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

empty return statement should be a compiler error when return type is specified
5 participants